﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Server.Models;
using System.Data;
using System.Data.SqlClient;
using Server.Util;

namespace Server.AdsService
{
    public class AdsDAL
    {
        DBUtil dbCtrl = new DBUtil();
        public void AdsAdd(AdsManageModel ads)
        {
            try
            {
                string sqlstr = " insert into  Manage_Ads(AIndex,ShowTime,OverDate,PictureUrl,NetUrl) values(@AIndex,@ShowTime,@OverDate,@PictureUrl,@NetUrl)";
                SqlParameter[] op = new SqlParameter[]
                {
                    new SqlParameter {SqlDbType = SqlDbType.Int ,ParameterName = "@AIndex",Value = ads.AIndex },
                    new SqlParameter {SqlDbType = SqlDbType.Int,ParameterName = "@ShowTime", Value = ads.ShowTime},
                    new SqlParameter {SqlDbType = SqlDbType.Int,ParameterName = "@OverDate", Value = ads.OverDate},
                    new SqlParameter {SqlDbType = SqlDbType.VarChar ,ParameterName = "@PictureUrl",Value = ads.PictureUrl },
                    new SqlParameter {SqlDbType = SqlDbType.VarChar ,ParameterName = "@NetUrl",Value = ads.NetUrl },
                };
                dbCtrl.Exec(sqlstr, op);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void AdsEdit(AdsManageModel ads)
        {
            try
            {
                string sqlstr = " update  Manage_Ads set AIndex=@AIndex,ShowTime=@ShowTime,OverDate=@OverDate,PictureUrl=@PictureUrl,NetUrl=@NetUrl where id = @id";
                SqlParameter[] op = new SqlParameter[]
                {
                    new SqlParameter {SqlDbType = SqlDbType.Int ,ParameterName = "@AIndex",Value = ads.AIndex },
                    new SqlParameter {SqlDbType = SqlDbType.Int,ParameterName = "@ShowTime", Value = ads.ShowTime},
                    new SqlParameter {SqlDbType = SqlDbType.Int,ParameterName = "@OverDate", Value = ads.OverDate},
                    new SqlParameter {SqlDbType = SqlDbType.VarChar ,ParameterName = "@PictureUrl",Value = ads.PictureUrl },
                    new SqlParameter {SqlDbType = SqlDbType.VarChar ,ParameterName = "@NetUrl",Value = ads.NetUrl },
                    new SqlParameter {SqlDbType = SqlDbType.Int ,ParameterName = "@id",Value = ads.id },
                };
                dbCtrl.Exec(sqlstr, op);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void AdsDel(int id)
        {
            try
            {
                string sqlstr = " delete from Manage_Ads where id =" + id;
                dbCtrl.Exec(sqlstr);
            }
            catch (Exception)
            {
                throw;
            }


        }
        public DataTable GetAdsList()
        {
            try
            {
                string sqlstr = " select * from Manage_Ads ";
                DataTable dt = dbCtrl.Query(sqlstr);

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
